{extend name="v1/layout/base"}
{block name="content"}
<div class="layuimini-main">
  <script type="text/html" id="toolbar">
    <div class="layui-btn-container">
      <button class="layui-btn layui-btn-black  layui-btn-sm" lay-event="reseve"><i class="layui-icon">&#xe63d;</i></button>
    </div>
  </script>
  <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
</div>
{/block}
{block name="myScript"}
<script>
  layui.use(['form', 'table', 'laydate'], function() {
    var $ = layui.jquery,
      form = layui.form,
      laydate = layui.laydate,
      sub_order_sn = '{$sub_order_sn}',
      table = layui.table;
    table.render({
      elem: '#currentTableId',
      url: '/{:config("manage.manage_name")}/{:config("manage.version")}/User/agentOrderShow',
      toolbar: '#toolbar',
      defaultToolbar: ['filter'],
      where: {sub_order_sn: sub_order_sn},
      cols: [
        [{
            field: 'user_name',
            width: '10%',
            title: '昵称'
          },
          {
            field: 'sub_order_sn',
            width: '15%',
            title: '关联单号'
          },
          {
            field: 'user_sn',
            width: '10%',
            title: '会员号'
          },
          {
            field: 'money',
            width: '10%',
            title: '结算金额'
          },
          {
            field: 'act',
            width: '10%',
            title: '结算类型'
          },
          {
            field: 'achievement_price',
            width: '10%',
            title: '结算业绩'
          },
          {
            field: 'level_name',
            width: '10%',
            title: '代理等级'
          },
          {
            field: 'is_finance',
            title: '是否出账',
            width: '10%'
          },

          {
            field: 'finance_time',
            minWidth: '200',
            fixed: 'right',
            align: "center",
            title: '出账时间'
          }
        ]
      ],
      limits: [20, 150, 200, 250, 500, 1000],
      limit: 20,
      id: 'table_ids',
      page: true,
      skin: 'line'
    });

    // 顶部操作工具
    table.on('toolbar(currentTableFilter)', function(obj) {
      var url = $(this).attr('data-url'),
        keywords = $("#keywords").val(),
        id_arr = getCheckValue();
      switch (obj.event) {
        case 'reseve':
          table.reload('table_ids');
          break;
        case 'export':
        var keywords = $("#keywords").val(),
          is_avg_finance = '',
          is_avg_finance_arr = [],
          is_check = '',
          is_check_arr = [],
          create_time = $("#create_time").val(),
          avg_finance_time = $("#avg_finance_time").val(),
          check_time = $("#check_time").val();
        $("input[name=is_avg_finance]:checked").each(function() {
          is_avg_finance_arr.push($(this).val());
        });
        is_avg_finance = is_avg_finance_arr.join(',');
        $("input[name=is_check]:checked").each(function() {
          is_check_arr.push($(this).val());
        });
        is_check = is_check_arr.join(',');
          var url = "/{:config('manage.manage_name')}/{:config('manage.version')}/User/exportAgentOrder";
          https(url, {
            keywords: keywords,
            is_avg_finance: is_avg_finance,
            is_check: is_check,
            create_time: create_time,
            avg_finance_time: avg_finance_time,
            check_time: check_time
          }, 'post', true, '正在执行订单数据导出').then(res => {
            if (res.result_code == 200) {
              var html = '<h1 style="padding: 0 20px;color:red;font-size:18px;margin-bottom:15px;">如果据量大于1万条系统自动分表导出</h1>';
              res.resultData.forEach(function(value, index) {
                html += '<h2 style="padding: 0 20px;font-size:16px;">第' + (index + 1) + '个订单文件：<a href="' + value + '">' + '下载' + '</a></h2>';
              })
              layer.open({
                type: 1,
                skin: 'layui-layer-rim', //加上边框
                area: ['400px', '200px'], //宽高
                content: html
              });
            }
          })
          break;

        case 'search':
          $("#xk-search-status").removeClass('xk-search-status');
          break;
      }
    })
    // 列表工具栏目
    table.on('tool(currentTableFilter)', function(obj) {
      var data = obj.data,
        event = obj.event,
        url = $(this).attr('data-url');
        var keywords = $("#keywords").val(),
          is_avg_finance = '',
          is_avg_finance_arr = [],
          is_check = '',
          is_check_arr = [],
          create_time = $("#create_time").val(),
          avg_finance_time = $("#avg_finance_time").val(),
          check_time = $("#check_time").val();
        $("input[name=is_avg_finance]:checked").each(function() {
          is_avg_finance_arr.push($(this).val());
        });
        is_avg_finance = is_avg_finance_arr.join(',');
        $("input[name=is_check]:checked").each(function() {
          is_check_arr.push($(this).val());
        });
        is_check = is_check_arr.join(',');
      url = url + '/sub_order_sn/' + data.sub_order_sn;
      switch (event) {
        case 'show':
          var index = layer.open({
            title: '代理订单'+data.sub_order_sn+'详情',
            type: 2,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: url,
            cancel: function() {
              table.reload('table_ids', {
                where: {
                  keywords: keywords,
                  is_avg_finance: is_avg_finance,
                  is_check: is_check,
                  create_time: create_time,
                  avg_finance_time: avg_finance_time,
                  check_time: check_time
                }
              });
            }
          });
          break;
      }
    })
    // 执行搜索
    $(".xk-dosearch").click(function() {
      var keywords = $("#keywords").val(),
        is_avg_finance = '',
        is_avg_finance_arr = [],
        is_check = '',
        is_check_arr = [],
        create_time = $("#create_time").val(),
        avg_finance_time = $("#avg_finance_time").val(),
        check_time = $("#check_time").val();
      $("input[name=is_avg_finance]:checked").each(function() {
        is_avg_finance_arr.push($(this).val());
      });
      is_avg_finance = is_avg_finance_arr.join(',');
      $("input[name=is_check]:checked").each(function() {
        is_check_arr.push($(this).val());
      });
      is_check = is_check_arr.join(',');
      table.reload('table_ids', {
        page: {
          curr: 1
        },
        where: {
          keywords: keywords,
          is_avg_finance: is_avg_finance,
          is_check: is_check,
          create_time: create_time,
          avg_finance_time: avg_finance_time,
          check_time: check_time
        }
      }, 'data');
    })
    // 获取选中项
    function getCheckValue() {
      var id_arr = [],
        id_str = '';
      var checkStatus = table.checkStatus('table_ids'),
        data = checkStatus.data;
      data.map(function(d) {
        id_arr.push(d.user_label_id);
      })
      id_str = id_arr.join(',');
      return id_str;
    }
  });
</script>
{/block}
